package org.school.service.impl;

import org.school.entity.Dingdan;
import org.school.dao.DingdanDao;
import org.school.service.DingdanService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * (Dingdan)表服务实现类
 *
 * @author makejava
 * @since 2023-01-14 13:57:08
 */
@Service("dingdanService")
public class DingdanServiceImpl implements DingdanService {
    @Resource
    private DingdanDao dingdanDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Dingdan queryById(Integer id) {
        return this.dingdanDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param dingdan 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<Dingdan> queryByPage(Dingdan dingdan, PageRequest pageRequest) {
        long total = this.dingdanDao.count(dingdan);
        return new PageImpl<>(this.dingdanDao.queryAllByLimit(dingdan, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param dingdan 实例对象
     * @return 实例对象
     */
    @Override
    public Dingdan insert(Dingdan dingdan) {
        this.dingdanDao.insert(dingdan);
        return dingdan;
    }

    /**
     * 修改数据
     *
     * @param dingdan 实例对象
     * @return 实例对象
     */
    @Override
    public Dingdan update(Dingdan dingdan) {
        this.dingdanDao.update(dingdan);
        return this.queryById(dingdan.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.dingdanDao.deleteById(id) > 0;
    }
}
